package src.demo;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;

import java.io.IOException;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2025/2/28 23:05
 */
public class DeepSeekCompletion {

    public static void main(String[] args) throws IOException {

//        test1();

        test2();
    }

    private static void test2() throws IOException {

        String base_url="https://api.deepseek.com/chat/completions";
        String content = "{\n" +
                "  \"messages\": [\n" +
                "    {\n" +
                "      \"content\": \"You are a helpful assistant\",\n" +
                "      \"role\": \"system\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"content\": \"public json\",\n" +
                "      \"role\": \"assistant\",\n" +
                "      \"prefix\": false\n" +
                "    }\n" +
                "  ],\n" +
                "  \"model\": \"deepseek-chat\",\n" +
                "  \"frequency_penalty\": 0,\n" +
                "  \"max_tokens\": 1024,\n" +
                "  \"presence_penalty\": 0,\n" +
                "  \"response_format\": {\n" +
                "    \"type\": \"json_object\"\n" +
                "  },\n" +
                "  \"stop\": null,\n" +
                "  \"stream\": false,\n" +
                "  \"stream_options\": null,\n" +
                "  \"temperature\": 1,\n" +
                "  \"top_p\": 1,\n" +
                "  \"tools\": null,\n" +
                "  \"tool_choice\": \"none\",\n" +
                "  \"logprobs\": false,\n" +
                "  \"top_logprobs\": null\n" +
                "}";

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, content);
        Request request = new Request.Builder()
                .url(base_url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", "Bearer sk-c82f8a3406a448448f9dc7af5b6370ab")
                .build();
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        System.out.println(response);
        System.out.println(result);






    }


    private static void test1() {
        try {
            long start = System.currentTimeMillis();

            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, "{\n  \"model\": \"deepseek-chat\",\n  \"prompt\": \"public static void main, \",\n  \"echo\": false,\n  \"frequency_penalty\": 0,\n  \"logprobs\": 0,\n  \"max_tokens\": 1024,\n  \"presence_penalty\": 0,\n  \"stop\": null,\n  \"stream\": false,\n  \"stream_options\": null,\n  \"suffix\": null,\n  \"temperature\": 1,\n  \"top_p\": 1\n}");
            Request request = new Request.Builder()
                    .url("https://api.deepseek.com/beta/completions")
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .addHeader("Authorization", "Bearer sk-c82f8a3406a448448f9dc7af5b6370ab")
                    .build();
            Response response = client.newCall(request).execute();


            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            JSONArray choices = jsonObject.getJSONArray("choices");

            String s = choices.get(0).toString();
            System.out.println(JSONObject.parseObject(s).getString("text"));

            long end = System.currentTimeMillis();

            System.out.println(end-start);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
